package engine.scene
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.event.ScenePlayerEvent;
	import engine.flag.GameDefine;
	import engine.flag.ObjType;
	import engine.scene.player.PlayerModel;
	
	import flash.display.Stage;
	import flash.events.Event;
	
	import newx.StageInfo;
	
	/**
	 *	舞台元素队列 
	 * @author test
	 * 
	 */
	public class SceneQueueManager
	{
		private static var _self	:SceneQueueManager;
		
		//单针处理数量
		private var _operNum : int = 2;
		
		private var _model : ModelLocator;
		
		public static function getInstance():SceneQueueManager
		{
			if ( _self == null )
			{
				_self = new SceneQueueManager();
			}
			return _self;
		}
		
		private var _stage			:Stage;
		
		private var _sceneManager	:SceneManager;
		
		public var _queueLists	:Array;
		/**一帧处理OBJ对象的个数***/
		private var _frameQueueList:Array;
		
		public function SceneQueueManager()
		{
			//			_operNum		=	int(StaticIni.getIniVar("OperNum", "num"));
			
			_stage			=	StageInfo.stage;
			_sceneManager	=	SceneManager.getInstance();
			_queueLists		=	[];
			_frameQueueList = 	[];
			
			_model			=	ModelLocator.getInstance();
		}
		
		
		
		private function addListener():void
		{
			_stage.addEventListener(Event.ENTER_FRAME, onFrame);
			
		}
		
		private function removeListener():void
		{
			if(_stage.hasEventListener(Event.ENTER_FRAME))
			{
				_stage.removeEventListener(Event.ENTER_FRAME, onFrame);	
			}
		}
		
		private function onFrame(evt : Event):void
		{
			var obj		: SceneObject; 
			//			_frameQueueList[0] _queueLists.shift();
			for(var i : int = 0; i < _queueLists.length; i++)
			{
				if(-1 != _operNum && i > _operNum) break;
				//				
				obj = _queueLists.shift();
				//以下类型obj显示在场景上
				//				if(obj is PlayerModel && GameData.SceneType!=GameDefine.SceneType_ForbiddenAreaScene)return;
				_model.dispatchEvent( new ScenePlayerEvent( ScenePlayerEvent.ADD_NPC, obj ) );
				
			}
			
			if(_queueLists.length == 0)
			{
				_queueLists	=	[];
				removeListener();
			}
		}
		
		public function addSceneObject(obj : SceneObject):void
		{
			var index : int = _queueLists.indexOf(obj);
			if(-1 != index)
			{
				_queueLists.splice(index, 1);
			}
			
			_queueLists.push(obj);
			
			if(_queueLists.length > 0)
			{
				addListener();
			}
		}
		
		public function delSceneObject(obj : SceneObject):void
		{
			var index : int = _queueLists.indexOf(obj);
			if(-1 != index)
			{
				_queueLists.splice(index, 1);
			}
			
			if(_queueLists.length == 0)
			{
				removeListener();
			}
			
			var type : int = obj.Type;
			if(type	==	ObjType.Player)
			{
				var tempName:String	=	obj.GetPropertiesEx("Name");
			}
			
			_model.dispatchEvent( new ScenePlayerEvent( ScenePlayerEvent.REMOVE_NPC, obj ) );
		}
		
		public function updataSceneObject(pName : String, obj : SceneObject):void
		{
			switch( pName )
			{
				case "Model":
					//				{//模型
					//					addSceneObject(obj);
					//					break;
					//				}
				case "FollowPet":
				case "FollowPetName":
				case "FollowPetGrade":
				case "FollowPetLevel":
				case "CantVisible":
				case "GameAFKStatus":
				case "HP":
				case "MaxHP":
				case "WorldPlayerDead":
				case "ModelEx":
				case "ClotheModel":
				case "ChallengeIndex":
				case "SnowBallCount":
				case "GuildName":
				{
					_model.dispatchEvent( new ScenePlayerEvent( ScenePlayerEvent.UPDATA_NPC, [ obj, pName ]) );
					break;
				}
					
			}
		}
	}
}